<template>
  <div>
    <h1>器械归还</h1>
    <el-card>
      <el-form :model="returnForm" label-width="120px" :rules="rules" ref="returnFormRef">
        <el-form-item label="设备ID" prop="deviceId">
          <el-input-number v-model="returnForm.deviceId" :min="1" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitReturn">提交归还申请</el-button>
          <el-button @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { add } from '@/api/applyrequest'
import { ElMessage } from 'element-plus'
import { useUserStore } from '@/store/user'

const userStore = useUserStore()

const returnForm = ref({
  deviceId: null
})

const rules = {
  deviceId: [{ required: true, message: '请输入设备ID', trigger: 'blur' }]
}

const returnFormRef = ref(null)

const submitReturn = () => {
  returnFormRef.value.validate(valid => {
    console.log(userStore)
    if (valid) {
      const formData = {
        ...returnForm.value,
        userId: userStore.userId
      }
      add(formData).then(() => {
        ElMessage.success('归还申请提交成功')
        resetForm()
      })
    }
  })
}

const resetForm = () => {
  returnFormRef.value.resetFields()
}
</script>

<style scoped>
.el-card {
  margin-top: 20px;
}
.el-form {
  width: 600px;
}
</style>